package cn.argentoaskia.crm.service.page.impl;

import cn.argentoaskia.crm.bean.EmployeeWithDeptName;
import cn.argentoaskia.crm.dao.EmployeeDAO;
import cn.argentoaskia.crm.service.page.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

// 把对象放进容器中
@Service
public class EmployeeServiceImpl implements EmployeeService {

    //  从容器中获取EmployeeDAO接口的代理对象
    @Autowired
    private EmployeeDAO employeeDAO;


    @Override
    public long selectByPage(int page, int limit, List<EmployeeWithDeptName> retList) {
        // pagehelper插件怎么用（mybatis的拦截器）
        // startPage(当前的页数，一页有多少条数据)
        // 1.开启分页（startPage方法）
        PageHelper.startPage(page, limit);
        // 2.执行DAO查询方法
        List<EmployeeWithDeptName> employeeWithDeptNames = employeeDAO.selectByPage();
        retList.addAll(employeeWithDeptNames);

        // 3.创建PageInfo类的对象，通过getTotal()方法拿到全部的数据总量
        //    SELECT COUNT(1)
        //    FROM employee e join department d on d.id = e.departmentId
        // (page-1) * limit = begin: page --> begin
        // LIMIT {begin} {limit}
        PageInfo<EmployeeWithDeptName> pageInfo = new PageInfo<>(employeeWithDeptNames);
        long totalCount = pageInfo.getTotal();
        return totalCount;
    }
}
